import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:provide/provide.dart';
import '../../provide/cart.dart';
import '../../model/cartInfo.dart';
import './cart_count.dart';

//购物车每个子项
class CartItem extends StatelessWidget {

  final CartInfoModel item;
  CartItem(this.item);
  @override
  Widget build(BuildContext context) {
    // print(item);
    return Container(
      margin: EdgeInsets.fromLTRB(5.0, 2.0, 5.0, 2.0),
      padding: EdgeInsets.fromLTRB(5.0, 10.0, 5.0, 10.0),
      decoration: BoxDecoration(
        color:Colors.white,
        border: Border(
          bottom: BorderSide(width: 1,color: Colors.black12)
        )
      ),
      child: Row(
        children: <Widget>[
          _cartCheckBt(context,item),
          _cartImage(),
          _cartGoodsName(),
          _cartPrice(context,item),
        ],
      ),

    );
  }


  //复选按钮
  Widget _cartCheckBt(context,item){
    return Container(
      child: Checkbox(
        // value: true,       //写死的
        value: item.isCheck, //把provide传过来的isCheck给复选框
        activeColor: Colors.pink,
        onChanged: (bool val){
          item.isCheck = val;
          Provide.value<CartProvide>(context).changeCheckState(item);
        },
      ),
    );
  }

  //商品图片 
  Widget _cartImage(){
    return Container(
      width: ScreenUtil().setWidth(150),
      padding: EdgeInsets.all(3.0),
      decoration: BoxDecoration(
        border: Border.all(width: 1,color: Colors.black12)
      ),
      child: Image.network(item.images),
    );
  }

  //商品名称
  Widget _cartGoodsName(){
    return Container(
      width: ScreenUtil().setWidth(300),
      padding: EdgeInsets.all(10),
      alignment: Alignment.topLeft, //顶端左对齐
      child: Column(
        children: <Widget>[
          Text(item.goodsName),
           CartCount(item)
        ],
      ),
    );
  }

  //商品价格
  Widget _cartPrice(context,item){
    return Container(
      width: ScreenUtil().setWidth(150),
      alignment: Alignment.centerRight,
      child: Column(
        children: <Widget>[
          Text('￥${item.price}'),
          Container(
            padding: EdgeInsets.only(top: 20),
            child: InkWell(
              onTap: (){
               Provide.value<CartProvide>(context).deleteOneGoods(item.goodsId);
              },
              child: Icon(
                Icons.delete_forever,
                color:Colors.black26,
                size: 30,
              ),
            ),
          ),
        ],
      ),
    );
  }

}